﻿// Copyright (c) 2014 Jonathan Magnan (http://jonathanmagnan.com/).
// All rights reserved (http://zzzproject.com/sqlbulkcopy-extensions/).
// Licensed under MIT License (MIT) (https://zsqlbulkcopy.codeplex.com/license/).
using System.Data;
using System.Data.SqlClient;

namespace Z.Net.Extensions
{
    internal partial class SqlBulkOperation
    {
        /// <summary>
        ///     Executes the bulk action.
        /// </summary>
        public void ExecuteActionSingle()
        {
            if (InternalDataSource is DataTable)
            {
                if (DataRowState.HasValue)
                {
                    DataTable list = ((DataTable) InternalDataSource).GetChanges(DataRowState.Value);

                    if (list != null)
                    {
                        foreach (DataRow dr in list.Rows)
                        {
                            using (var sqlCommand = new SqlCommand(SqlActionBulk1, SqlConnection))
                            {
                                foreach (SqlBulkCopyColumnMapping param in MappingList)
                                {
                                    SqlDbType typeName = Setting.MappingColumnWithDbType[param.DestinationColumn];
                                    var parameter = new SqlParameter {ParameterName = "@" + ParameterMapping[param.DestinationColumn], SqlDbType = typeName, Value = dr[param.SourceColumn]};
                                    sqlCommand.Parameters.Add(parameter);
                                }
                                sqlCommand.Transaction = SqlTransaction;
                                sqlCommand.ExecuteNonQuery();
                            }
                        }
                    }
                }
                else
                {
                    var list = (DataTable) InternalDataSource;
                    foreach (DataRow dr in list.Rows)
                    {
                        using (var sqlCommand = new SqlCommand(SqlActionBulk1, SqlConnection))
                        {
                            foreach (SqlBulkCopyColumnMapping param in MappingList)
                            {
                                SqlDbType typeName = Setting.MappingColumnWithDbType[param.DestinationColumn];
                                var parameter = new SqlParameter {ParameterName = "@" + ParameterMapping[param.DestinationColumn], SqlDbType = typeName, Value = dr[param.SourceColumn]};
                                sqlCommand.Parameters.Add(parameter);
                            }
                            sqlCommand.Transaction = SqlTransaction;
                            sqlCommand.ExecuteNonQuery();
                        }
                    }
                }
            }
            else if (InternalDataSource is DataRow[])
            {
                var list = (DataRow[]) InternalDataSource;
                foreach (DataRow dr in list)
                {
                    using (var sqlCommand = new SqlCommand(SqlActionBulk1, SqlConnection))
                    {
                        foreach (SqlBulkCopyColumnMapping param in MappingList)
                        {
                            SqlDbType typeName = Setting.MappingColumnWithDbType[param.DestinationColumn];
                            var parameter = new SqlParameter {ParameterName = "@" + ParameterMapping[param.DestinationColumn], SqlDbType = typeName, Value = dr[param.SourceColumn]};
                            sqlCommand.Parameters.Add(parameter);
                        }
                        sqlCommand.Transaction = SqlTransaction;
                        sqlCommand.ExecuteNonQuery();
                    }
                }
            }
            else if (InternalDataSource is IDataReader)
            {
                var list = (IDataReader) InternalDataSource;
                while (list.Read())
                {
                    using (var sqlCommand = new SqlCommand(SqlActionBulk1, SqlConnection))
                    {
                        foreach (SqlBulkCopyColumnMapping param in MappingList)
                        {
                            SqlDbType typeName = Setting.MappingColumnWithDbType[param.DestinationColumn];
                            var parameter = new SqlParameter {ParameterName = "@" + ParameterMapping[param.DestinationColumn], SqlDbType = typeName, Value = list[param.SourceColumn]};
                            sqlCommand.Parameters.Add(parameter);
                        }

                        sqlCommand.Transaction = SqlTransaction;
                        sqlCommand.ExecuteNonQuery();
                    }
                }
            }
        }
    }
}